//
//  CZMorePush2ViewController.m
//  A01-CZ彩票
//
//  Created by apple on 15-6-16.
//  Copyright (c) 2015年 itcast. All rights reserved.

#import "CZMorePush2ViewController.h"
#import "CZItemLabel.h"
#import "CZKeyboard.h"
@interface CZMorePush2ViewController () <CZKeyboardDelegate>
@property (nonatomic, weak) UIDatePicker *datePicker;
@property (nonatomic, strong) NSIndexPath *indexPath;
@end

@implementation CZMorePush2ViewController

- (void)setData{
    //第一组
    CZItem *item11 = [CZItemSwitch itemWithTitle:@"推送我关注的比赛"];
    
    CZGroup *group1 = [CZGroup groupWithFooter:@"开启后，当有新中奖订单，打开应用时会显示动画提醒我。为避免显示过于频繁，高频彩不会提醒" items:@[item11]];
    
    
    //第二组
    CZItem *item21 = [CZItemLabel itemWithTitle:@"起始时间" time:@"00:00"];
    CZGroup *group2 = [CZGroup groupWithHeader:@"我是组头" items:@[item21]];
    
    //第三组
    CZItem *item31 = [CZItemLabel itemWithTitle:@"结束时间" time:@"00:00"];
    CZGroup *group3 = [CZGroup groupWithItems:@[item31]];
    
    self.groups = @[group1,group2,group3];
}


//9.1 点击cell
//9.2 点击cell弹出 datePicker
//9.3 设置datePicker上面显示确定和取消按钮
//9.4 点击确定按钮。设置时间



//9.1 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.indexPath = indexPath;
    //获取当前点击的cell
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    //为了点击cell的时候能弹出键盘
    //生成文本框，并且让文本框成为第一响应者
    UITextField *txt = [UITextField new];
    [cell addSubview:txt];
    
    //设置键盘的工具栏
    CZKeyboard *tool = [CZKeyboard keyboardTool];
    tool.delegate = self;
    txt.inputAccessoryView = tool;
    
   
    //弹出datePicker
    UIDatePicker *datePicker = [[UIDatePicker alloc] init];
    self.datePicker = datePicker;
    datePicker.datePickerMode = UIDatePickerModeTime;
    datePicker.backgroundColor = [UIColor lightGrayColor];
    
    //把弹出键盘改成datePicker
    txt.inputView = datePicker;
    
    //让文本框成为第一响应者，此处之前 应该先设置inputView
    [txt becomeFirstResponder];
}

//代理方法
- (void)keyboardDidClickedSure:(CZKeyboard *)kb{
    //1 获取选中的时间
    NSDateFormatter *ndf = [NSDateFormatter new];
    ndf.dateFormat = @"HH:mm";
    NSString *time = [ndf stringFromDate:self.datePicker.date];
    
    //2 获取对应的模型
    CZGroup *group = self.groups[self.indexPath.section];
    CZItemLabel *item = group.items[self.indexPath.row];
    item.time = time;
    
    //3 重新加载当前行
    [self.tableView reloadRowsAtIndexPaths:@[self.indexPath] withRowAnimation:UITableViewRowAnimationNone];
    
}

- (void)keyboardDidClickedCancel:(CZKeyboard *)kb{
    //取消键盘
    [self.view endEditing:YES];
}

@end
